using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AudioMovement : MonoBehaviour {

	public enum State {
		Dialog,
		Moving,
		Waiting,
		Victory,
		Death
	};

	State state = State.Dialog;

	public GameObject player;
	PlayerMovement playerScript;

	public List<AudioClip> goodLines; // Default lines for keeping up the good work, the end is near, etc.
	public List<AudioClip> badLines; // Hey where are you going? You are going away from me, my friend. It is dangerous here - it is better you stick wih me, mate.
	public List<AudioClip> staticLines; // when you are standing
	public List<AudioClip> dangerousLines; // Hey, what the fuck you are doing? ARe you already to die?
	List<AudioClip> activeLines;
	string activeLinesName = "good";

	public float speed = 1f;
	public float radius = 1f;
	ArrayList checkpoints;
	int activeCheckpoint = 1;

	AudioSource voiceSource;
	//VoiceLines voiceLines;
	public AudioSource footSource;

	float lastStateCheckTime = 0f;
	float statusCheckDuration = 1f;
	float lastDistance = 0f;

	// Use this for initialization
	void Start () {
		checkpoints = new ArrayList();
		activeLines = goodLines;
	
		GameObject[] sceneObjects = GameObject.FindObjectsOfType(typeof(GameObject)) as GameObject[];
		foreach(GameObject go in sceneObjects)
			if (go.name.StartsWith("Checkpoint"))
				checkpoints.Add(go);

		player = GameObject.Find("Player");
		playerScript = GameObject.FindObjectOfType(typeof(PlayerMovement)) as PlayerMovement;
	}
	
	// Update is called once per frame
	void Update () {

		if (state == State.Dialog)
		{
			if (!audio.isPlaying)
				state = State.Moving;
		}
		if (state == State.Moving)
		{
			Vector3 dist = GetActiveCheckpoint().transform.position - transform.position;
			Vector3 dir = dist.normalized;
			transform.Translate(dir * Time.deltaTime * speed);

			if (dist.magnitude > 0f)
			{
				if (!footSource.isPlaying)
					footSource.Play();
			}
			else 
				footSource.Pause();

			if (dist.magnitude <= radius)
			{
				footSource.Pause();
				state = State.Waiting;
			}


		}
		if (state == State.Waiting)
		{
			// Should do the looping sounds
			UpdateStatus();
			UpdateVoiceLines();
		}

		if (state == State.Victory)
			UpdateVoiceLines();

	}

	public int GetActiveCheckpointID()
	{
		return activeCheckpoint;
	}

	GameObject GetActiveCheckpoint()
	{
		foreach (GameObject go in checkpoints)
			if (go.name == "Checkpoint" + activeCheckpoint)
				return go;

		return null;
	}

	public void ActivateNextCheckpoint()
	{
		state = State.Dialog;

		if (++activeCheckpoint == checkpoints.Count)
			state = State.Victory;

		StartCoroutine(PlayVoice());

	}

	public void UpdateVoiceLines()
	{
		if (!audio.isPlaying)
		{
			if (state == State.Victory)
					Application.LoadLevel("VictoryScene");

			if (state == State.Dialog)
				state = State.Moving;

			int id = Random.Range(0, activeLines.Count-1);
			audio.clip = activeLines[id];
			audio.Play();
		}
	}

	public void UpdateStatus()
	{
		// Always bad. Only react on player moves
		if (Time.time > lastStateCheckTime + statusCheckDuration)
		{
			lastStateCheckTime = Time.time;

			float dist = Vector3.Distance(transform.position, player.transform.position);
			if (Mathf.Approximately(dist, lastDistance))
				SetActiveLines("static");
			else if (dist < lastDistance)
				SetActiveLines("good");
			else 
				SetActiveLines("bad");

			if (playerScript.isInDanger())
				SetActiveLines("danger");

			lastDistance = dist;
		}
	}

	public IEnumerator PlayVoice()
	{
		yield return !audio.isPlaying;
		AudioSource audioSource = GetActiveCheckpoint().GetComponent<AudioSource>();
		audio.clip = audioSource.clip;
		audio.Play();
	}

	public State GetState()
	{
		return state;
	}

	public void SetState(State newState)
	{
		state = newState;
	}

	public void PlayGrats()
	{
		int id = Random.Range(0, goodLines.Count);
		audio.clip = goodLines[id];
		audio.Play();
	}

	public void SetActiveLines(string lines)
	{
		if (lines == "danger" && activeLinesName != "danger")
			activeLines = dangerousLines;
		else if (lines == "good" && activeLinesName != "good")
			activeLines = goodLines;
		else if (lines == "bad" && activeLinesName != "bad")
			activeLines = badLines;
		else if (lines == "static" && activeLinesName != "static")
			activeLines = staticLines;

		activeLinesName = lines;
	}

}
